﻿Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_Config_Detail
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            If Not Request.QueryString("Id").ToString.Equals("0") Then
                hidID.Value = Request.QueryString("Id")
                ShowData()
            End If
        End If
    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            Dim _Huyen = (From h In data.Provinces Where h.ParentId = 171 Select h).ToList
            With ddlHuyen
                .DataSource = _Huyen
                .DataTextField = "Title"
                .DataValueField = "Id"
                .DataBind()
                .Items.Insert(0, New ListItem("---Chọn---", ""))
            End With

            Dim p As Config = (From q In data.Configs Where q.Id = hidID.Value Select q).SingleOrDefault
            If Not p Is Nothing Then
                ddlHuyen.SelectedValue = IIf(IsNothing(p.ProvinceId) = True, "", p.ProvinceId)
                txtBuoitrahoso.Text = IIf(IsNothing(p.BuoiTraHoSo) = True, "", p.BuoiTraHoSo)
                If p.Ref1 <> "" Then
                    If p.Ref1 = 1 Then
                        chkIsBangHieu.Checked = True
                    Else
                        chkIsBangHieu.Checked = False
                    End If
                End If

            End If
        End Using
    End Sub
#End Region
#Region "Event for control"
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Dim p As Config = (From q In data.Configs Where q.Id = hidID.Value).SingleOrDefault
            Try
                p.Id = hidID.Value
                p.ProvinceId = ddlHuyen.SelectedValue
                p.BuoiTraHoSo = txtBuoitrahoso.Text.Trim()
                Dim _IsBangHieu = chkIsBangHieu.Checked
                If _IsBangHieu Then
                    p.Ref1 = 1
                Else
                    p.Ref1 = 0
                End If
                data.SaveChanges()
                'Insert_App_Log("Insert Config:" & txtTitle.Text.Trim & "", Function_Name.Config, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Config/List.aspx';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        ShowData()
    End Sub
    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBack.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
